package com.beasts.ant.page.controller.content;

import com.beasts.ant.page.common.dto.AjaxDTO;
import com.beasts.ant.page.entity.content.ApArticle;
import com.beasts.ant.page.service.content.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 内容管理 - 文章管理
 *
 * @author Locker
 * @date 25/06/2018 10:30 AM
 * @since 1.0
 */
@Controller
@RequestMapping("/content/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @RequestMapping("/queryAll")
    @ResponseBody
    public AjaxDTO quereArticles(
            @RequestParam(name = "page", required = false, defaultValue = "1") String pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") String pageSize
    ) {
        AjaxDTO ajaxDTO = new AjaxDTO();
        System.out.println(pageNum + " " + pageSize);
        ajaxDTO.setData(articleService.queryArticles());
        return ajaxDTO;
    }

    @RequestMapping("/insert")
    @ResponseBody
    public AjaxDTO insertArticle(HttpServletRequest req) {
        ApArticle apArticle = new ApArticle(req);
        articleService.insert(apArticle);
        return new AjaxDTO(null, "insert ");
    }

    @RequestMapping("/remove")
    @ResponseBody
    public AjaxDTO removeArticle(@RequestParam(name = "id") String id) {
        articleService.remove(id);
        return new AjaxDTO(null, "remove " + id);
    }

    @RequestMapping("/removes")
    @ResponseBody
    public AjaxDTO removeArticles(@RequestParam(name = "ids[]") String[] ids) {
        articleService.removes(Arrays.asList(ids));
        return new AjaxDTO(null, "removes " + ids.length);
    }

    @RequestMapping("/update")
    @ResponseBody
    public AjaxDTO updateArticle(HttpServletRequest req) {
        ApArticle apArticle = new ApArticle(req);
        articleService.update(apArticle);
        return new AjaxDTO(null, "update");
    }

    @RequestMapping("/uploadFile")
    @ResponseBody
    public AjaxDTO uploadFile(HttpServletRequest req) {

        String a = req.getParameter("a");

        return new AjaxDTO(null, "uploadFile");
    }

}
